package com.planet.classes;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class EventHandler extends DefaultHandler {
	private Events _events;
	private Event _event;
	private String _current;
	
	public EventHandler(){}
	
	public Events GetEvents(){
		return _events;
	}
	
	@Override
	public void startDocument() throws SAXException{
		_events = new Events();
	}
	
	@Override
	public void endDocument() throws SAXException{}
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException{
		if(localName.equals("event")){
			_event = new Event();
		}
	}
	
	@Override
	public void characters(char ch[], int start, int length){
		_current = new String(ch, start, length);
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException{
				
		if(localName.equals("id")){
			_event.set_id(Integer.parseInt(_current));
		}
		else if(localName.equals("location_id")){
			_event.set_location(Integer.parseInt(_current));
		}
		else if(localName.equals("date")){
			_event.set_date(_current);
		}
		else if(localName.equals("begin_main")){
			_event.set_start(_current);
		}
		else if(localName.equals("title_main")){
			_event.set_title(_current);
		}
		else if(localName.equals("price_pre")){
			_event.set_price_pre(Double.valueOf(_current).doubleValue());
		}
		else if(localName.equals("price_event")){
			_event.set_price_event(Double.valueOf(_current).doubleValue());
		}
		else if(localName.equals("infotext")){
			_event.set_info(_current);
		}
		else if(localName.equals("image1")){
			_event.set_image(_current);
		}
		else if(localName.equals("ticket")){
			_event.set_url(_current);
		}
		else if(localName.equals("event")){
			_events.addEvent(_event);
		}
	}	
}